package com.olympus.hermes.service;

import com.olympus.hermes.bean.dto.member.CreateMemberDTO;
import com.olympus.hermes.bean.dto.member.OutputMemberDTO;
import com.olympus.hermes.common.service.AbstractService;
import com.olympus.hermes.common.support.PageRequest;
import com.olympus.hermes.repo.MemberRepo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author Mickey
 * @since 2021/2/1
 */
@Service
@AllArgsConstructor
public class MemberService extends AbstractService {

    private final MemberRepo memberRepo;

    private final PasswordEncoder passwordEncoder;

    public void create(CreateMemberDTO createMember) {
        creatorFor(memberRepo)
                .instance(createMember::convertTo)
                //密码加密
                .update(member -> member.setPassword(passwordEncoder.encode(member.getPassword())))
                .call();
    }

    public Page<OutputMemberDTO> list(PageRequest pageRequest) {
        return memberRepo
                .findAll(pageRequest)
                .map(member -> new OutputMemberDTO().reverse().convert(member));
    }


}
